<?php
/**
 * [LupMisNotAllowedSync]
 * 本代码为系统自动生成代码，请根据自己业务进行修改;
 * 生成时间 2022-02-18 18:16:27;
 * 版权所有 2020-2022 lizhongwen，并保留所有权利;
 * 说明: 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布;
 * 作者: 中文Lee;
 * 作者主页: http://www.lizhongwen.com;
 * 邮箱: 360811363@qq.com;
 * QQ: 360811363;
 */

namespace modules\sys\service\impl;

use application\core\SnowFlake;
use Exception;
use modules\sys\component\SetCacheComponent;
use modules\sys\mapper\ActionMapper;
use modules\sys\mapper\PageMapper;
use modules\sys\service\ActionService;

class ActionServiceImpl implements ActionService
{

    /**
     * @param array $data
     * @return int
     */
    public function add(array $data): int
    {
        $data['id'] = SnowFlake::instance()->nextId();
        $add = ActionMapper::add($data);
        if ($add == 0) {
            return 0;
        }
        SetCacheComponent::action();
        return $add;
    }

    /**
     * @param array $data
     * @return int
     */
    public function edit(array $data): int
    {
        $edit = ActionMapper::edit($data);
        if ($edit == 0) {
            return 0;
        }
        SetCacheComponent::action();
        return $edit;
    }

    /**
     * @param $data
     * @param $pagePars
     * @return array
     */
    public function lists($data, $pagePars): array
    {
        $pageMapper = new PageMapper();
        $pageRs = $pageMapper->lists();
        $pageRs = array_column($pageRs, NULL, "id");
        $actionRs = ActionMapper::lists($data, $pagePars);
        foreach ($actionRs['rows'] as $k => $v) {
            $actionRs['rows'][$k]['page_row'] = $pageRs[$v['page_id']];
        }
        return $actionRs;
    }

    /**
     * @param array $data
     * @return array
     */
    public function query(array $data): array
    {
        return ActionMapper::query($data);
    }

    /**
     * @param array $data
     * @return int
     * @throws \Exception
     */
    function del(array $data): int
    {
        $ids = $data['ids'];
        if (empty($ids)) {
            throw new Exception("请至少选择一条数据");
        }
        $idsArr = explode(",", $ids);
        $delNum = 0;
        foreach ($idsArr as $id) {
            $delNum += ActionMapper::del(['id' => $id]);
        }
        return $delNum;

    }
}

